using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.MsgServices.Dtos;
using IM.Easy.MsgServices.IServices;
using IM.Easy.Entity.Msg;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Core.AppContext;
using IM.Easy.Core.Attributes;

namespace IM.Easy.Api.Controllers.Msg
{
    /// <summary>
    /// 收件箱 控制器
    /// </summary>
    [Route("msg/emails/inbox")]
    public class EmailRecivesController : ControllerBase
    {
        private readonly IEmailRecivesService _service;
        private readonly IEmailAccountService _emailAccountService;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public EmailRecivesController(IEmailRecivesService service, IEmailAccountService emailAccountService)
        {
            _service = service;
            _emailAccountService = emailAccountService;
        }

        /// <summary>
        /// 获取所有EmailRecives对象
        /// </summary>
        /// <returns>返回EmailRecives列表</returns>
        [HttpGet]
        [Api(Name = "获取收件箱", Perm = "msg:emailinbox:list")]
        public PageResponse<EmailRecives> GetList([FromQuery] EmailRecivesQueryDto query)
        {
            var user = ImApp.User;
            var emailAccount = _emailAccountService.GetAccount(user.Id);
            query.AudienceId = emailAccount.Id;
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 批量删除EmailRecives对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        [Api(Name = "删除收件箱", Perm = "msg:emailinbox:del")]
        public void Delete([FromBody] long[] ids)
        {
            _service.Delete(ids);
        }

        /// <summary>
        /// 读取邮件
        /// </summary>
        [HttpGet("read/{id}")]
        [Api(Name = "读取邮件", Perm = "msg:emailinbox:read")]
        public EmailRecives Read([FromRoute] long id)
        {
            var result = _service.GetById(id);
            _service.SetRead(id);
            return result;
        }

    }
}
